package com.drug.lib.view.recycptr;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

import java.util.List;

public abstract class ItemViewBinder<T, V extends ViewHolder> {

    DynamicRecyclerAdapter mAdapter;

    protected long getItemId(@NonNull T item) {
        return RecyclerView.NO_ID;
    }

    protected final int getPosition(@NonNull final ViewHolder holder) {
        return holder.getAdapterPosition();
    }

    public DynamicRecyclerAdapter getmAdapter() {
        if (mAdapter == null) {
            throw new IllegalStateException("ItemViewBinder " + this + " not attached to DynamicRecyclerAdapter. " +
                    "You can't call the method before registering the binder.");
        }
        return mAdapter;
    }

    protected abstract @NonNull V onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent);


    protected abstract void onBindViewHolder(@NonNull V holder, @NonNull T entity);

    protected void onBindViewHolder(@NonNull V holder, @NonNull T entity, @NonNull List<Object> payloads) {
        onBindViewHolder(holder, entity);
    }

    protected void onViewAttachedToWindow(@NonNull V holder) {}

    protected void onViewDetachedFromWindow(@NonNull V holder) {}
}
